#include <algorithm>
#include <iostream>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <alpha/crypto/hash/md5.hpp>
#include <alpha/crypto/encoding/hex.hpp>

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		std::cout << "Usage: md5sum FILE" << std::endl;
		return 1;
	}
	namespace io = boost::iostreams;
	try
	{
		std::cout.fill('0');
		std::cout.width(2);
		io::stream<io::file_descriptor_source> in(argv[1]);
		io::filtering_ostream out(alpha::crypto::md5()
			| alpha::crypto::hex_encoder()
			| std::ostream_iterator<char>(std::cout));
		io::copy(in, out);
		out.reset();
		std::cout << " *"<< argv[1] << std::endl;
	}
	catch (...)
	{
		std::cerr << "IO error" << std::endl;
	}
}
